package com.kordar.admin.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kordar.admin.enums.CommonActive;
import com.kordar.admin.modular.sys.entity.TenantEntity;
import com.kordar.admin.modular.sys.service.DictService;
import com.kordar.admin.modular.sys.service.TenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Map;

@Slf4j
@Controller
public class DefaultController {

    final
    DictService dictService;

    final
    TenantService tenantService;

    public DefaultController(DictService dictService, TenantService tenantService) {
        this.dictService = dictService;
        this.tenantService = tenantService;
    }

    private Map<String, String> webconfig;

    @ModelAttribute("webconfig")
    public Map<String, String> webconfig() {
        webconfig = dictService.config("WEB-CONFIG");
        log.info(webconfig.toString());
        return webconfig;
    }

    @GetMapping("/")
    public String index() {
        return "pages/index";
    }

    @GetMapping("/login")
    public String login(Model model) {
        model.addAttribute("bg", webconfig.getOrDefault("LOGIN-BG", "/assets/common/images/bg-login.jpg"));
        String tenant = webconfig.get("TENANT");
        model.addAttribute("tenant", tenant != null);
        if (tenant != null) {
            model.addAttribute("tenantList", tenantService.list(new QueryWrapper<TenantEntity>()
                    .eq("active", CommonActive.OPEN.getCode())
            ));
        }
        return "pages/login";
    }

    @GetMapping("/index")
    public String home(Model model) {
        return webconfig.getOrDefault("TEMPLATE_HOME", "pages/home");
    }

    @GetMapping("/sys/theme")
    public String theme() {
        return "pages/modular/frame/theme";
    }

    @GetMapping("/sys/lock")
    public String lock(Model model) {
        model.addAttribute("bg", webconfig.getOrDefault("LOCK-SCREEN-BG", "/assets/common/images/bg-screen.jpg"));
        return "pages/modular/frame/lock-screen";
    }

    @GetMapping("/sys/user-chpwd")
    public String chpwd() {
        return "pages/modular/frame/reset-pwd";
    }
}
